﻿#include "MeasureView/MeasureArc3P.h"
#include "MeasureView/UnitConvert.h"
#include "MeasureView/MeasureAlgorithm.h"
#include "MeasureView/MeasureDataSet.h"
#include <QtMath>

namespace Qly {

MeasureArc3P::MeasureArc3P(MeasureDataSet *parent)
    :MeasureElement(parent)
{
    m_measureType = Arc3P;
    m_results_pix.resize(6);
    m_results_um.resize(6);
    m_keyPoints_pix.resize(4);// 第四个存圆心坐标
    m_keyPoints_um.resize(4);
    m_textTemplate = parent->m_textTemplate_MeasureArc3P;
}


int MeasureArc3P::mouseLButtonPress(QPointF point_pix, QPointF point_um)
{
    if( m_measureState == DS_IDLE )
    {
        m_keyPoints_pix[0] = point_pix;
        m_keyPoints_um[0] = point_um;
        m_measureState = DS_STATE1;
        return m_measureState;
    }
    if(m_measureState == DS_STATE1)
    {
        m_keyPoints_pix[1] = point_pix;
        m_keyPoints_um[1] = point_um;
        m_measureState = DS_STATE2;
        return m_measureState;
    }
    if(m_measureState == DS_STATE2)
    {
        m_keyPoints_pix[2] = point_pix;
        m_keyPoints_um[2] = point_um;
        compute();
        if(m_results_pix[2] == 0)
        {
            m_measureState = DS_ERROR;
            return m_measureState;
        }
        else
        {
            updateText(true);

            m_measureState = DS_FINISH;
            return m_measureState;
        }
    }
    return m_measureState;
}

void MeasureArc3P::compute()
{

    CircleMeasure alg;

    if( alg.buildBy3Points(m_keyPoints_pix[0], m_keyPoints_pix[1], m_keyPoints_pix[2]) )
    {
        m_results_pix[0] = alg.m_centre.x();
        m_results_pix[1] = alg.m_centre.y();
        m_results_pix[2] = alg.m_radius;
        m_results_pix[3] = m_results_pix[2] * 2.0;
        m_results_pix[4] = alg.m_theta[0];
        m_results_pix[5] = alg.m_det_theta[1];

//        m_measureState = DS_FINISH;
    }

    //double ps = LensManager::getInstance().pixelSize_um();
    m_results_um[0] = m_pParent->m_pixSize_um * m_results_pix[0];
    m_results_um[1] = m_pParent->m_pixSize_um * m_results_pix[1];
    m_results_um[2] = m_pParent->m_pixSize_um * m_results_pix[2];
    m_results_um[3] = m_results_um[2] * 2;
    m_results_um[4] = m_results_pix[4];
    m_results_um[5] = m_results_pix[5];

    m_keyPoints_pix[3] = QPointF(m_results_pix[0], m_results_pix[1]);
    m_keyPoints_um[3] = QPointF(m_results_um[0], m_results_um[1]);

}

int MeasureArc3P::mouseMove(QPointF point_pix, QPointF point_um)
{
    if(m_measureState == DS_STATE2)
    {
        m_keyPoints_pix[2] = point_pix;
        m_keyPoints_um[2] = point_um;
        compute();
        return 3;
    }
    return 0;
}


//int MeasureArc3P::setLens(int oldLens, int newLens, int method)
//{
//    if(oldLens == newLens || oldLens == -1) return 0;// 这时不需要更新任何数据
//    if(m_keyPoints_pix.size() != m_keyPoints_um.size()) //这里表示出现了错误，也应该退出
//    {
//        return 0;
//    }
//    LensManager & lens = LensManager::getInstance();
//    if(method == 0)//只更新长度的数值，图元在图像上的位置不变
//    {
//        double new_pixelSize = lens.currentLens().pixelSize_um;
//        for(int  i = 0; i < m_keyPoints_pix.size(); ++i)
//        {
//            m_keyPoints_um[i] = m_keyPoints_pix[i] * new_pixelSize;
//        }
//    }
//    else
//    {
//        LensData & oldData = lens.lensByIndex(oldLens);
//        LensData & newData = lens.currentLens();
//        double dx = (newData.xCompensate_um - oldData.xCompensate_um);
//        double dy = (newData.yCompensate_um - oldData.yCompensate_um);
//        for(int  i = 0; i < m_keyPoints_pix.size(); ++i)
//        {
//            m_keyPoints_pix[i] = (m_keyPoints_um[i] + QPointF(dx, dy)) / newData.pixelSize_um;
//        }
//    }
//    compute();
//    updateText();
//    return 1;
//}

void MeasureArc3P::paint(QPainter &painter)
{
    if( m_measureState != DS_IDLE )
    {
        painter.save();
        painter.setPen(m_pen);
        int x, y, width, height;
        x = m_results_pix[0] - m_results_pix[2];
        y = m_results_pix[1] - m_results_pix[2];
        width = m_results_pix[3];
        height = m_results_pix[3];

        double theta1 = m_results_pix[4] * 180 / M_PI * 16;
        double det_theta = m_results_pix[5] * 180 / M_PI * 16;
        painter.drawArc ( x, y, width, height, theta1, det_theta );
        if( m_measureState == DS_STATE1 )
        {
            drawCross(painter, m_keyPoints_pix[0]);
        }
        else if( m_measureState == DS_STATE2  || m_measureState == DS_FINISH)
        {
            drawCross(painter, m_keyPoints_pix[0]);
            drawCross(painter, m_keyPoints_pix[1]);
            drawCross(painter, m_keyPoints_pix[2]);

            drawCross(painter, QPointF(m_results_pix[0], m_results_pix[1]));

            QPen p = painter.pen();
            p.setStyle(Qt::DashDotLine);
            painter.setPen(p);
            painter.drawLine(m_keyPoints_pix[0], QPointF(m_results_pix[0], m_results_pix[1]));
            painter.drawLine(m_keyPoints_pix[2], QPointF(m_results_pix[0], m_results_pix[1]));
            m_textBlock.draw(painter);
            if(m_textMoving)
            {
                QPen pen = m_pen;
                pen.setStyle(Qt::DashDotLine);
                painter.setPen(pen);
                QPointF p = m_textBlock.anchorPoint();
                painter.drawLine(p, m_keyPoints_pix[3]);
                painter.setPen(m_pen);
            }
        }
        painter.restore();
    }
}

void MeasureArc3P::updateText(bool updatePosition)
{
    double dia = m_results_um[3];
    double radius = m_results_um[2];

    QString text = m_textTemplate;
    QString diaStr, radStr, unitStr;
    QString angleStr1 = QString::number(m_results_pix[5] * 180 / M_PI, 'g', 4);
    QString angleStr2 = QString::number(m_results_pix[5], 'g', 4);
    double arcLength = m_results_pix[5] * radius;

    m_pParent->m_unitConvert_MeasureArc3P.replaceLength(text, dia, "@diameter", "@unit");
    m_pParent->m_unitConvert_MeasureArc3P.replaceLength(text, radius, "@radius", "@unit");
    m_pParent->m_unitConvert_MeasureArc3P.replaceLength(text, fabs(arcLength), "@arcLength", "@unit");

    text.replace("@angleDeg", angleStr1, Qt::CaseSensitive);
    text.replace("@angle", angleStr2, Qt::CaseSensitive);

    QString str3 = QString::number(m_id);
    text.replace("@id", str3, Qt::CaseSensitive);

    m_textBlock.setText(text);

    if(updatePosition)
    {
        QPointF p_pix = QPointF(m_results_pix[0], m_results_pix[1]);
        QPointF p_um = QPointF(m_results_um[0], m_results_um[1]);
        m_textBlock.moveTo(p_pix, p_um);
    }
}
}
